//package org.vs.controller;
//
//import lombok.AllArgsConstructor;
//import org.springframework.beans.factory.annotation.Autowired;
//import org.springframework.web.bind.annotation.*;
//import org.vs.result.RestResult;
//import org.vs.result.RestResultBuilder;
//import org.vs.service.BlockchainService;
//import org.web3j.crypto.*;
//import org.web3j.protocol.Web3j;
//import org.web3j.protocol.core.DefaultBlockParameterName;
//import org.web3j.protocol.core.methods.request.Transaction;
//import org.web3j.protocol.core.methods.response.*;
//import org.web3j.protocol.exceptions.TransactionException;
//import org.web3j.tx.Transfer;
//import org.web3j.utils.Convert;
//import org.web3j.utils.Numeric;
//
//import javax.annotation.Resource;
//import java.io.IOException;
//import java.math.BigDecimal;
//import java.math.BigInteger;
//import java.security.InvalidAlgorithmParameterException;
//import java.security.NoSuchAlgorithmException;
//import java.security.NoSuchProviderException;
//import java.util.concurrent.ExecutionException;
//
//@RestController
//@RequestMapping("/eth")
//@AllArgsConstructor
//public class BlockchainController {
//    private Web3j web3j;
//    private BlockchainService blockchainService;
//    private Credentials credentials;
//
//    @GetMapping("height")
//    public long getHeight() throws IOException {
//        EthBlockNumber blockNumber = web3j.ethBlockNumber().send();
//        long blockHeight = blockNumber.getBlockNumber().longValue();
//        return blockHeight;
//    }
//
//    @GetMapping("/select/balance/{address}")
//    public RestResult selectBalance(@PathVariable String address) throws IOException {
//        return new RestResultBuilder().success((Object) blockchainService.selectBalance(address));
//    }
//
//    @GetMapping("/get/balance")
//    public RestResult getGenerateBalance() throws IOException {
//        return new RestResultBuilder().success((Object) blockchainService.selectBalance(credentials.getAddress()));
//    }
//
//    @PostMapping("/miner/{address}")
//    public RestResult miner(@PathVariable String address) throws IOException {
//        if (blockchainService.miner(address)) {
//            return new RestResultBuilder().success(address + " 账户开始挖矿");
//        }
//
//        return new RestResultBuilder().fail(address + " 账户挖矿失败");
//    }
//
//    @PostMapping("/deployment/contract")
//    public RestResult deployment() throws Exception {
//        return new RestResultBuilder().success(blockchainService.deployment());
//    }
//
//    @PostMapping("/load/contract")
//    public RestResult load() throws Exception {
//        return new RestResultBuilder().success(blockchainService.load());
//    }
//
//
//    @GetMapping("/get/net/version")
//    public RestResult getNetVersion() throws IOException {
//        return new RestResultBuilder().success(web3j.netVersion().send().getNetVersion());
//    }
//
//    @PostMapping("/create/account")
//    public RestResult createAccount() throws Exception {
//        ECKeyPair ecKeyPair = Keys.createEcKeyPair();
//        BigInteger privateKeyInDec = ecKeyPair.getPrivateKey();
//        String privateKey = privateKeyInDec.toString(16);
//        WalletFile aWallet = Wallet.createLight("123", ecKeyPair);
//        String address = aWallet.getAddress();
//        if (address.startsWith("0x")) {
//            address = address.substring(2).toLowerCase();
//        } else {
//            address = address.toLowerCase();
//        }
//        address = "0x" + address;
//        System.out.println("地址：" + address);
//        System.out.println("秘钥：" + privateKey);
//
//        return new RestResultBuilder().success();
//    }
//
//    @PostMapping("/trans/to")
//    public RestResult transTo() throws Exception {
//        BigDecimal amount = new BigDecimal("0.01");
//
//        BigInteger value = Convert.toWei(amount, Convert.Unit.ETHER).toBigInteger();
//        //不是必须的 可以使用默认值
//        BigInteger gasLimit = BigInteger.ONE;
////						getTransactionGasLimit(transaction);
//        //不是必须的 缺省值就是正确的值
//        BigInteger nonce = getTransactionNonce("0x147928e51332fa5378e24ce575a5a927dbd458c6");
//        //该值为大部分矿工可接受的gasPrice
//        BigInteger gasPrice = Convert.toWei(BigDecimal.valueOf(5), Convert.Unit.GWEI).toBigInteger();
//        Transaction transaction = makeTransaction(
//                "0x147928e51332fa5378e24ce575a5a927dbd458c6",
//                credentials.getAddress(),
//                nonce, gasPrice,
//                gasLimit, value);
//        EthSendTransaction ethSendTransaction = web3j.ethSendTransaction(transaction).send();
//        String txHash = ethSendTransaction.getTransactionHash();
//
//        return new RestResultBuilder().success();
//    }
//
//    private BigInteger getTransactionNonce(String address) {
//        BigInteger nonce = BigInteger.ZERO;
//        try {
//            EthGetTransactionCount ethGetTransactionCount = web3j.ethGetTransactionCount(address, DefaultBlockParameterName.PENDING).send();
//            nonce = ethGetTransactionCount.getTransactionCount();
//        } catch (IOException e) {
//            e.printStackTrace();
//        }
//        return nonce;
//    }
//
//    private Transaction makeTransaction(String fromAddress, String toAddress,
//                                               BigInteger nonce, BigInteger gasPrice,
//                                               BigInteger gasLimit, BigInteger value) {
//        Transaction transaction;
//        transaction = Transaction.createEtherTransaction(fromAddress, nonce, gasPrice, gasLimit, toAddress, value);
//        return transaction;
//    }
//
//    @GetMapping("/current/gas")
//    public RestResult getCurrentGas() throws ExecutionException, InterruptedException {
//        EthGasPrice ethGasPrice = web3j.ethGasPrice().sendAsync().get();
//        return new RestResultBuilder().success(ethGasPrice.getGasPrice());
//    }
//}
